/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.PropChangeHandler;
import org.jbox2d.common.Vec2D;
import se.krka.kahlua.vm.JavaFunction;
import se.krka.kahlua.vm.LuaCallFrame;


public final class Camera extends GameObject{
    public Camera() throws WorldsException
    {
        super("Camera");
        addPropChangeHandler("ScaleFactor", new PropChangeHandler()
        {

            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(isCurrent())
                    Main.getView().scaleFactor = (Float) value;
            }

        });
        addPropChangeHandler("Offset", new PropChangeHandler()
        {

            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(isCurrent())
                    Main.getView().offset = (Vec2D) value;
            }

        });
        addPropChangeHandler("RotationOffset", new PropChangeHandler()
        {

            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(isCurrent())
                    Main.getView().rotateOffset = (float) Math.toRadians((Float)value);
            }

        });
        addPropChangeHandler("Position", new PropChangeHandler()
        {

            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(isCurrent())
                {
                    Main.getView().setCamPosition((Vec2D) value, false);
                    Main.getView().update();
                }
            }

        });
        addPropChangeHandler("FollowObject", new PropChangeHandler()
        {

            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(isCurrent())
                {
                    if(value == null)
                        Main.getView().followObject = null;
                    else if(value instanceof Part)
                    {
                        Main.getView().followObject = (Part) value;
                        Main.getView().setCamPosition(((Part)value).getPosition(), false);
                    }
                    else
                        throw new RuntimeException("FollowObject must be a Part");
                }
            }

        });
        addFunction("moveToCenter", new JavaFunction()
        {

            public int call(LuaCallFrame callFrame, int nArguments) {
                if(isCurrent())
                    Main.runInMainThread(new Runnable()
                    {
                        public void run() {
                            try {
                                zoomToCenter();
                            } catch (WorldsException ex) {
                                ex.printStackTrace();
                            }
                        }

                    });
                return 0;
            }

        });
        //x_;
    }
    private boolean isCurrent()
    {
        try {
            if(Main.noView || Main.isServer() && getParent() instanceof Player)
                return false;
            if(this == Main.getGame().currentCamera)
                return true;
            else if(Main.getGame().getWorlds().size() == 1 && getParent() instanceof Root)
            {
                    Main.log("Warning: Using game.Camera is deprecated, please use game.CurrentCamera or [player].Camera");
                    return true;
            }
            else
                return false;
        } catch (WorldsException ex) {
            return false;
        }
    }
    public void zoomToCenter() throws WorldsException
    {
        net.worlds.World w = Main.getGame().getCurrentWorld();
        Vec2D center;
        if(!Main.getGame().getCurrentWorld().getRootObject().hasChild("World"))
            return;
        if(w.getService("World").hasChild("Ground"))
            center = new Vec2D(w.getWidth()/2f, w.getService("World").getChild("Ground").getVec2DProp("Size").y + 15f);
        else
            center = new Vec2D(w.getWidth()/2, w.getHeight()/2);
        setProp("Position", center, true);
    }
}
